package com.zh.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zh.common.vo.Result;
import com.zh.modules.app.entity.BalanceHisDO;
import com.zh.modules.app.entity.UserDO;
import com.zh.modules.app.repository.BalanceHisRepository;
import com.zh.modules.app.repository.UserRepository;
import com.zh.modules.app.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/app/balance")
@Api(tags = "余额")
@Slf4j
public class BalanceController {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private BalanceHisRepository balanceHisRepository;

    @GetMapping("/get")
    @ApiOperation("余额查询")
    public Result<Long> get() {
        UserDO userDO = userRepository.getById(JwtUtils.getUser().getId());
        Long balance = userDO.getBalance();
        double balanceInYuan = (double) balance / 100.0; // 将分转换为元
        return Result.succeed(balanceInYuan);
    }

    @GetMapping("/queryHis")
    @ApiOperation("余额查询")
    public Result<List<BalanceHisDO>> queryHis() {
        Integer userId = JwtUtils.getUser().getId();
        List<BalanceHisDO> balanceHisDOS = balanceHisRepository.list(new LambdaQueryWrapper<BalanceHisDO>().eq(BalanceHisDO::getUserId, userId));
        return Result.succeed(balanceHisDOS);
    }
}
